<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" :page.sync="page" :permission="permissionList"
      :before-open="beforeOpen" v-model="form" ref="crud" @row-update="rowUpdate" @row-save="rowSave" @row-del="rowDel"
      @search-change="searchChange" @search-reset="searchReset" @selection-change="selectionChange"
      @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad">
      <!-- 自定义操作列 -->
      <template #menu="{ row, index, size }">
        <el-button type="primary" :size="'mini'" icon="el-icon-view" @click="rowView(row, index)" style="width: 23px;
        height: 22px; padding: 0;"></el-button>
        <!-- <el-button type="primary" :size="'mini'" icon="el-icon-edit" @click="rowEdit(row, index)" style="width: 23px;
        height: 22px; padding: 0;"></el-button>
        <el-button type="danger" :size="'mini'" icon="el-icon-delete" @click="rowDel(row, index)" style="width: 23px;
        height: 22px; padding: 0;"></el-button> -->
      </template>

    
    </avue-crud>
    <!-- 弹窗 -->
    <el-dialog :visible.sync="isShowOrderDetailModal" append-to-body destroy-on-close title="订单详情" width="60%">
      <avue-crud :option="detailOption" :data="detailData">
      </avue-crud>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShowOrderDetailModal = false"> 取消 </el-button>
        <el-button type="primary" @click="isShowOrderDetailModal = false"> 确认 </el-button>
      </div>
    </el-dialog>
  </basic-container>
</template>

<script>
import { getCourseOrderPageList, getCourseOrderItemDetails } from "./api/course";

import { mapGetters } from "vuex";
import website from "@/config/website";

export default {
  data() {
    return {
      isShowOrderDetailModal: false,
      detailData: [],
      detailOption: {
        menu: false,
        menuType: 'button',
        height: "auto",
        calcHeight: 30,
        tip: false, // 当前表格已选择多少项 提示语
        searchShow: false,
        searchMenuSpan: 6,
        border: false,

        index: true,
        indexLabel: '序号',
        viewBtn: false,
        editBtn: false,
        selection: false,
        dialogClickModal: false,
        addBtn: false,
        delBtn: false,
        column: [
          {
            label: "报名人",
            prop: "name",
          },
          {
            label: "报名人昵称",
            prop: "nickname",
          },
          {
            label: "联系电话",
            prop: "phone",
          },
          {
            label: "头像",
            prop: "profile_picture",
            type: "upload",
            clearable: true,
            loadText: '头像上传中，请稍等',
            listType: "picture-card",
            action: "/admin/common/upload",
            propsHttp: {
              url: 'file',
              home: $imgBaseUrl
            },
            dataType: "array",
          },
        ],
      },
      form: {},
      query: {},
      loading: true,
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
      },
      selectionList: [],
      option: {
        menuType: 'button',
        height: "auto",
        calcHeight: 30,
        tip: false, // 当前表格已选择多少项 提示语
        searchShow: true,
        searchMenuSpan: 6,
        border: false,

        index: true,
        indexLabel: '序号',
        searchBtnText: '提交',
        searchBtnIcon: '提交',
        emptyBtnText: '重置',
        emptyBtnIcon: '重置',
        viewBtn: false,
        editBtn: false,
        selection: false,
        dialogClickModal: false,
        addBtn: false,
        addBtnText: '添加',
        addBtnIcon: 'el-icon-plus',
        delBtn: false,
        column: [
          {
            label: "账号昵称",
            prop: "nickname",
            search: true,
            hide: true, //表格是否显示该字段（该字段的列）
            display: false //弹出的表单是否显示
          },
          {
            label: "活动标题",
            prop: "title",
            search: true,
            hide: true, //表格是否显示该字段（该字段的列）
            display: false //弹出的表单是否显示
          },
          {
            label: "订单编号",
            prop: "order_no",
            rules: [
              {
                required: true,
                message: "请输入订单编号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "订单类型",
            prop: "order_type",
            type: 'select',
            dicData: [{
              label: '活动订单',
              value: 1
            }, {
              label: '课程订单',
              value: 2
            }],
          },
          {
            label: "单价",
            prop: "unit_price",
          },
          {
            label: "数量",
            prop: "num",
          },
          {
            label: "订单总金额",
            prop: "total_price",
          },
          {
            label: "应付金额",
            prop: "pay_total_price",
          },
          {
            label: "实付金额",
            prop: "payer_total",
          },

          {
            label: "订单状态",
            prop: "order_status",
            type: 'select',
            search: true,
            dicData: [{
              label: '待支付',
              value: 0
            }, {
              label: '支付成功',
              value: 1
            }, {
              label: '支付失败',
              value: 2
            },
            {
              label: '用户取消',
              value: 3
            },
            {
              label: '平台取消',
              value: 4
            }],
          },
          {
            label: "支付凭证",
            prop: "transaction_id",
            search: true,
            rules: [{
              required: true,
              message: "请输入支付凭证",
              trigger: "change"
            }],
          },

          {
            label: "支付时间",
            prop: "paid_at",
            type: "datetime",
            rules: [{
              required: true,
              message: "请选择支付时间",
              trigger: "change"
            }],
          },



          {
            label: "备注",
            prop: "remark",
            type: 'textarea',
            rules: [
              {
                required: true,
                message: "请输入活动备注",
                trigger: "blur",
              },
            ],
          },
          {
            label: "创建时间",
            prop: "created_at",
            display: false,
          },
        ],
      },
      data: [],
      userInfo: {}
    };
  },

  computed: {
    ...mapGetters(["permission"]),

    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.user_add, true),
        viewBtn: this.vaildData(this.permission.user_view, true),
        delBtn: this.vaildData(this.permission.user_delete, true),
        editBtn: this.vaildData(this.permission.user_edit, true),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.account_id);
      });
      return JSON.stringify(ids);
    },
  },
  created() {
    const user = localStorage.getItem(website.key + "-user");
    if (user) this.userInfo = JSON.parse(user);
  },
  mounted() { },
  methods: {


    rowView(row, index) {
      this.isShowOrderDetailModal = true
      // this.$refs.crud.rowView(row, index);
      getCourseOrderItemDetails({ order_id: row.id }).then((res) => {
        console.log('获取报名人详情', res.data);
        this.detailData = res.data;
      });
    },
    rowEdit(row, index) {
      this.$refs.crud.rowEdit(row, index);
    },
    rowDel(row, index) {
      this.$refs.crud.rowDel(row, index);
    },
    rowSave(row, done, loading) {
      return
      let param = {
        uid: this.userInfo.id,
        ...row
      }
      xxx(param).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      return
      let params = {
        "uid": this.userInfo.id,
        ...row
      }
      if (typeof row.province === 'object' && row.province !== null) {
        params.province = row.province.code
      }
      if (typeof row.city === 'object' && row.city !== null) {
        params.city = row.city.code
      }
      if (typeof row.area === 'object' && row.area !== null) {
        params.area = row.area.code
      }
      XXX(params).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowDel(row) {
      return
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return XXX({ id: row.id });
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return XXX(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(page_cursor) {
      this.page.currentPage = page_cursor;
    },
    sizeChange(page_size) {
      this.page.pageSize = page_size;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      let param = {
        ...params,
        page: page.currentPage,
        size: page.pageSize
      }
      getCourseOrderPageList(param).then((res) => {
        const resData = res.data;
        this.page.total = resData.total;
        this.data = resData.list;
        this.loading = false;
        this.selectionClear();
      });
    },


    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
  },
};
</script>


<style lang="scss" scoped></style>